package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import com.bdqn.util.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * IndexController
 *
 * @author LILIBO
 * @since 2024/7/27
 */
@Controller
public class IndexController {
    @Resource
    private SysUserService sysUserService;
    /**
     * 去登录页
     *
     * @return
     */
    @GetMapping("/login")
    public String toLogin() {
        // 加载页面渲染需要的资源
        return "login";
    }

    /**
     * 执行登录操作
     *
     * @return
     */
    @PostMapping("/login")
    public String doLogin(HttpServletRequest request, HttpSession session, @RequestParam("account") String account, @RequestParam("password") String password) {
        SysUser sysUser = sysUserService.login(account,password); // 此处省略部分代码...
        if (sysUser == null) {
            request.setAttribute("error", "用户名或密码错误");
            // redirect：重定向（让浏览器发起请求）
            // forward：转发（让服务器发起请求）
            return "/login";
        }
        session.setAttribute(Constants.USER_SESSION, sysUser);
        return "redirect:frame";
    }
     @GetMapping("/frame")
     public String toFrame(){
        return "frame";
     }


     @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
//        session.removeAttribute(Constants.USER_SESSION);
         return "redirect:login";
     }

//    @RequestMapping("/toMain")
//    public String main(HttpSession session){
//        if (session.getAttribute(Constants.USER_SESSION) == null){
//            return "redirect:toLogin";
//        }
//        return "frame";
//    }

}
